package com.zcxy.fast.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcxy.fast.bean.PageQuery;
import com.zcxy.fast.entity.SysDict;
import com.zcxy.fast.mapper.SysDictMapper;
import com.zcxy.fast.mybatisExtend.BaseServiceImpl;
import com.zcxy.fast.mybatisExtend.LambdaValiteCondition;
import com.zcxy.fast.service.ISysDictService;
import com.zcxy.fast.utils.BeanCopyUtil;
import com.zcxy.fast.vo.SysDictVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * 平台-字典表业务层处理
 *
 * @author zcc
 * @email 860474958@qq.com
 * @date 2020-12-18 15:05:58
 */
@Service
@Slf4j
public class SysDictServiceImpl extends BaseServiceImpl<SysDictMapper, SysDict> implements ISysDictService {

    @Override
    public IPage<SysDictVo> queryPageList(PageQuery<SysDict> queryVo) {
        SysDict sysDict = queryVo.getT() == null ? new SysDict() : queryVo.getT();
        QueryWrapper queryWrapper = new QueryWrapper(sysDict);
        Page page = new Page(queryVo.getPageNo(), queryVo.getPageSize());
        IPage<SysDict> sysUserVoIPage = baseMapper.selectPage(page, queryWrapper);
        return BeanCopyUtil.copyPage(sysUserVoIPage, SysDictVo.class);
    }

    @Override
    public List<SysDictVo> queryList(SysDict sysDict) {
        SysDict sysDictq = sysDict == null ? new SysDict() : sysDict;
        QueryWrapper queryWrapper = new QueryWrapper(sysDictq);
        List<SysDictVo> SysDictVos = BeanCopyUtil.copyListToList(baseMapper.selectList(queryWrapper), SysDictVo.class);
        return SysDictVos;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addSysDict(SysDict sysDict) {
        LambdaValiteCondition<SysDict> valiteCondition = LambdaValiteCondition.newInstance(sysDict);
        valiteCondition.addField(SysDict::getName).addErrInfo("名称不能重复");
        validFiledExist(valiteCondition);
        valiteCondition.clear();
        valiteCondition.addField(SysDict::getCode).addErrInfo("编码不能重复");
        validFiledExist(valiteCondition);
        baseMapper.insert(sysDict);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSysDict(SysDict sysDict) {
        LambdaValiteCondition<SysDict> valiteCondition = LambdaValiteCondition.newInstance(sysDict);
        valiteCondition.addField(SysDict::getName).addErrInfo("名称不能重复");
        valiteCondition.addExcludes(SysDict::getId);
        validFiledExist(valiteCondition);
        valiteCondition.clear();
        valiteCondition.addField(SysDict::getCode).addErrInfo("编码不能重复");
        valiteCondition.addExcludes(SysDict::getId);
        validFiledExist(valiteCondition);
        baseMapper.updateById(sysDict);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeAllSysDict(List<String> ids) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in("id", ids);
        baseMapper.delete(queryWrapper);
    }


}
